<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class meal_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//使用套餐，写入套餐扣取记录
	public function consume($order_id) {
		$sql = "SELECT * FROM jf_order_project WHERE order_id='{$order_id}' AND meal_id>0";
		//echo $sql;
		$projects = $this->db->get_results($sql);
		if ($projects) {
			foreach ($projects as $k => $project) {
				$sql = "SELECT id FROM jf_member_meal WHERE meal_id='{$project->meal_id}' 
					AND member_id='{$this->member_id}' AND num>0 
					ORDER BY num ASC";
				$member_meal_id = intval($this->db->get_var($sql));
				if ($member_meal_id) {
					$sql = "UPDATE jf_member_meal SET num=num-1 WHERE id='{$member_meal_id}'";
					$this->db->query($sql);
					//写入扣取记录
					$sql = "INSERT INTO jf_meal_history(meal_id, member_id, add_time, order_id, project_id, title) 
						VALUES('{$project->meal_id}', '{$this->member_id}', '".time()."', '{$order_id}', '{$project->project_id}', '{$project->project_name}')";
					$this->db->query($sql);
				}
			}
		}
	}

	public function status_name($status) {
		$str = $status;
		switch($status){
			case 0:$str = '隐藏';break;
			default:$str = '正常';break;
		}
		return $str;
	}	
}
